// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef FULLY_HOMOMORPHIC_ENCRYPTION_TRANSPILER_COMMON_RUNNER_H_
#define FULLY_HOMOMORPHIC_ENCRYPTION_TRANSPILER_COMMON_RUNNER_H_

// Enccode/decode struct fields according to their order in the C++ declaration
// (DECLARATION), or reverse-declaration order (REVERSE).  The latter order is
// issued by the XLS toolchain when emitting XLS IR code.  The former is used
// when we convert XLS IR to Verilog netlists.
//
// UNDEFINED is an invalid setting.
//
// We maintain a single static flag of type StructEncodeOrder that we set at
// runtime, before code generated by the struct transpiler starts relying on the
// value.
enum class StructEncodeOrder { UNDEFINED = -1, REVERSE, DECLARATION };

// Retrieve the value of the static flag maintained.  This method will assert
// when ther oder is UNDEFINED.
StructEncodeOrder GetStructEncodeOrder();

// Set the order in the static flag.  Do not use this method.  Use one of the
// two setters below.
void SetStructEncodeOrder(StructEncodeOrder order);

// Declare a static object of this type to set the encoding order to REVERSE.
struct StructReverseEncodeOrderSetter {
  StructReverseEncodeOrderSetter() {
    SetStructEncodeOrder(StructEncodeOrder::REVERSE);
  }
};

// Declare a static object of this type to set the encoding order to
// DECLARATION.
struct StructDeclarationEncodeOrderSetter {
  StructDeclarationEncodeOrderSetter() {
    SetStructEncodeOrder(StructEncodeOrder::DECLARATION);
  }
};

#endif  // FULLY_HOMOMORPHIC_ENCRYPTION_TRANSPILER_COMMON_RUNNER_H_
